<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\service;

use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * 会员数据初始化
 * Class MemberService
 * @package app\store\service
 */
class ConsumeService
{
    /**
     * 数据处理
     * @param array $data 会员数据
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function buildConsumeList(&$list)
    {
        $mids = array_unique(array_column($list, 'uid'));
        $goodsIds = array_unique(array_column($list, 'goods_id'));
        $mealIds = [];
        foreach($list as $value) {
            if($value['type'] == 2) {
                $mealIds[] = $value['goods_id'];
            }
        }

        $memberList = Db::name("StoreMember")->whereIn('id', $mids)->select();
        $goodsList = Db::name('StoreGoods')->whereIn('id', $goodsIds)->select();
        $mealList = Db::name('StoreMeal')->whereIn('id', $mealIds)->select();
        foreach ($list as $key => $vo) {
            list($list[$key]['nickname'], $list[$key]['goods'], $list[$key]['mela'], $list[$key]['member']) = ['', [], [], [], []];
            foreach ($memberList as $member) {
                ($vo['uid'] === $member['id']) && $list[$key]['member'] = $member;
            }
            foreach ($goodsList as $goods) {
                ($vo['goods_id'] === $goods['id']) && $list[$key]['goods'] = $goods;
            }
            if($vo['type'] == 2) {
                foreach ($mealList as $meal) {
                    ($vo['goods_id'] == $meal['id']) && $list[$key]['meal'] = $meal;
                }
            }
        }
        return $list;
    }

}